package com.mehdiuz.backend.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mehdiuz.backend.role.entity.Menu;
import com.mehdiuz.backend.role.entity.vo.MenuBackVo;
import com.mehdiuz.backend.role.entity.vo.MetaVo;
import com.mehdiuz.backend.role.entity.vo.RouterVo;
import com.mehdiuz.backend.role.service.MenuService;
import com.mehdiuz.backend.role.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 23548
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2022-10-31 19:18:50
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = menuMapper.selectList(new QueryWrapper<Menu>());
        List<Menu> list = menuList.stream().filter(menu -> {
            return menu.getParentId() == 0;
        }).collect(Collectors.toList());
        list.forEach(l->{
            findChildren(l,menuList);
        });
        return list;
    }

    @Override
    public void findChildren(Menu menu,List<Menu> menuList){
        List<Menu> children = new ArrayList<>();
        menuList.forEach(m->{
            if(m.getParentId().equals(menu.getId())){
                children.add(m);
                findChildren(m,menuList);
            }
        });
        menu.setChildren(children);
    }

    private static RouterVo defaultRouter(Menu menu){
        RouterVo routerVo = new RouterVo();
        routerVo.setHidden(false);
        routerVo.setAlwaysShow(false);
        routerVo.setComponent(menu.getComponent());
        if(menu.getType()==0){
            routerVo.setPath("/" + menu.getPath());
        }else {
            routerVo.setPath(menu.getPath());
        }
        routerVo.setMeta(new MetaVo(menu.getName(),menu.getIcon()));
        List<RouterVo> children = new ArrayList<>();
        routerVo.setChildren(children);
        return routerVo;
    }

    //将传入的menus按照router组装成树形结构
    @Override
    public List<RouterVo> assemblyRouter(List<Menu> menus) {
        menus.removeIf(menu -> menu.getType()==2);
        List<Menu> directory = menus.stream().filter(menu -> menu.getType() == 0).collect(Collectors.toList());
        List<RouterVo> routerVoList = directory.stream().map(menu -> {
            RouterVo routerVo = defaultRouter(menu);
            findRouterChildren(routerVo, menu, menus);
            return routerVo;
        }).collect(Collectors.toList());
        return routerVoList;
    }

    private void findRouterChildren(RouterVo routerVo,Menu menu,List<Menu> menus){
        menus.forEach(m->{
            if(m.getParentId().equals(menu.getId())){
                RouterVo vo = defaultRouter(m);
                routerVo.getChildren().add(vo);
                findRouterChildren(vo,m,menus);
            }
        });
    }
}




